package com.lindroy.jetpacknotes.lifecycle.lifecycle_custom

import android.app.Activity
import android.os.Bundle
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import com.lindroy.jetpacknotes.R
import com.lindroy.jetpacknotes.lifecycle.lifecycle_ui.new_way.LocationListener2

class CustomLifecycleActivity : Activity(), LifecycleOwner {

    private val lifecycleRegistry by lazy { LifecycleRegistry(this) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_custom_lifecycle)
        lifecycleRegistry.addObserver(LocationListener2())
    }

    /**
     * 重写 onStart() 方法
     */
    override fun onStart() {
        super.onStart()
        lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
    }

    override fun onStop() {
        super.onStop()
        lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
    }

    /**
     * 实现 getLifecycle() 返回 lifecycleRegistry 实例
     */
    override fun getLifecycle(): Lifecycle  = lifecycleRegistry

}